package com.iespring.service.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.goods.po.MallCategory;
import com.iespring.bean.admin.user.entity.MallGoods;
import com.iespring.bean.admin.user.entity.MallUser;
import com.iespring.bean.app.user.vo.*;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.*;
import com.iespring.mapper.app.MallGrouponMapper;
import com.iespring.mapper.app.MallIssueMapper;
import com.iespring.service.app.footprint.WxFootPrintService;
import com.iespring.service.app.search.WxSearchService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xD
 * @since 2022/12/31 21:43
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {
    @Resource
    private MallGoodsMapper mallGoodsMapper;
    @Resource
    private MallCategoryMapper mallCategoryMapper;
    @Resource
    private MallBrandMapper mallBrandMapper;
    @Resource
    private MallGoodsAttributesMapper mallGoodsAttributesMapper;
    @Resource
    private MallCommentMapper mallCommentMapper;
    @Resource
    private MallGrouponMapper mallGrouponMapper;
    @Resource
    private MallIssueMapper mallIssueMapper;
    @Resource
    private MallGoodsProductMapper mallGoodsProductMapper;
    @Resource
    private MallGoodsSpecificationsMapper mallGoodsSpecificationsMapper;
    @Resource
    private MallCollectMapper mallCollectMapper;
    @Resource
    private MallUserMapper mallUserMapper;

    //杨宇健 添加足迹信息
    @Resource
    private WxFootPrintService footPrintService;

    //张舒鹏 添加历史搜索记录
    @Resource
    private WxSearchService wxSearchService;

    @Override
    public Integer count() {
        return mallGoodsMapper.selectCount();
    }

    @Override
    public WxCategoryVo category(Integer id) {
        WxCategoryVo wxCategoryVo = new WxCategoryVo();
        MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(id);
        //不为零是currentId
        if (mallCategory.getPid() != 0) {
            wxCategoryVo.setParentCategory(mallCategoryMapper.selectParentByCurrentId(id));
            wxCategoryVo.setCurrentCategory(mallCategory);
            wxCategoryVo.setBrotherCategory(mallCategoryMapper.selectBortherByPrimaryKey(id));
        } else {
            //若为0,则说明传入的是parentId
            List<MallCategory> mallCategories = mallCategoryMapper.selectBortherByParentId(id);
            wxCategoryVo.setBrotherCategory(mallCategories);
            wxCategoryVo.setParentCategory(mallCategory);
            wxCategoryVo.setCurrentCategory(mallCategories.get(0));
        }
        return wxCategoryVo;
    }

    @Override
    public CommonData list(BasePageInfo pageInfo) {
        //开启分页功能, Integer categoryId, Integer brandId
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());
        List<WxMallGoodsVo> wxMallGoodsVos = null;
        if (!ObjectUtils.isEmpty(pageInfo.getCategoryId()) && pageInfo.getCategoryId() != 0) {
            wxMallGoodsVos = mallGoodsMapper.selectGoodsByCategoryIdLimitByPage(pageInfo.getCategoryId());
        } else if (!ObjectUtils.isEmpty(pageInfo.getBrandId())) {
            wxMallGoodsVos = mallGoodsMapper.selectGoodsByBrandId(pageInfo.getBrandId());
        } else if (!ObjectUtils.isEmpty(pageInfo.getKeyword())) {
            String keyword = pageInfo.getKeyword();
            try {
                String name = URLDecoder.decode(keyword, "utf-8");
                // >>>>> 张舒鹏 插入搜索记录 start
                wxSearchService.insertKeyword(name);
                // <<<<< 张舒鹏 end
                wxMallGoodsVos = mallGoodsMapper.selectGoodsByKeyWord(pageInfo, name);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        PageInfo<WxMallGoodsVo> goodsPageInfo = new PageInfo<>(wxMallGoodsVos);
        CommonData<WxMallGoodsVo> commonData = new CommonData<>();
        commonData.setFilterCategoryList(mallCategoryMapper.getGoodsCategoryInfo());
        commonData.setTotal((int) goodsPageInfo.getTotal());
        commonData.setPages(goodsPageInfo.getPages());
        commonData.setPage(goodsPageInfo.getPageNum());
        commonData.setLimit(goodsPageInfo.getPageSize());
        commonData.setList(goodsPageInfo.getList());
        return commonData;
    }

    @Transactional
    @Override
    public WxDetailVo detail(Integer id) {
        // >>>>> 杨宇健 添加足迹信息 start
        footPrintService.insert(id);
        // <<<<< 杨宇健 添加足迹信息 end

        WxDetailVo wxDetailVo = new WxDetailVo();
        wxDetailVo.setAttribute(mallGoodsAttributesMapper.selectByGoodsId(id));
        wxDetailVo.setBrand(mallBrandMapper.selectByGoodsId(id));
        MallCommentVo wxCommentVo = new MallCommentVo();
        List<WxCommentVo> wxCommentVos = mallCommentMapper.selectByValueId2(id);
        for (WxCommentVo commentVo : wxCommentVos) {
            if (!ObjectUtils.isEmpty(commentVo)) {
                MallUser mallUser = mallUserMapper.selectByPrimaryKey(commentVo.getUserId());
                commentVo.setNickname(mallUser.getNickname());
            }
        }
        wxCommentVo.setData(wxCommentVos);
        wxCommentVo.setCount(wxCommentVos.size());
        wxDetailVo.setComment(wxCommentVo);
        wxDetailVo.setGroupon(mallGrouponMapper.selectAll());
        MallGoods mallGoods = mallGoodsMapper.selectByPrimaryKey(id);
        wxDetailVo.setInfo(mallGoods);

        wxDetailVo.setIssue(mallIssueMapper.selectAll());
        wxDetailVo.setProductList(mallGoodsProductMapper.selectByGoodsId(id));
        if (ObjectUtils.isEmpty(mallGoods.getShareUrl())) {
            wxDetailVo.setShare(false);
        } else {
            wxDetailVo.setShare(true);
        }
        wxDetailVo.setShareImage(mallGoods.getShareUrl());

        List<String> names = mallGoodsSpecificationsMapper.selectSpecifications(id);
        List<WxMallGoodsSpecificationVo> list = new ArrayList<>();
        for (String name : names) {
            WxMallGoodsSpecificationVo wxMallGoodsSpecificationVo = new WxMallGoodsSpecificationVo();
            wxMallGoodsSpecificationVo.setValueList(mallGoodsSpecificationsMapper.selectBySpecification(name, id));
            wxMallGoodsSpecificationVo.setName(name);
            list.add(wxMallGoodsSpecificationVo);
        }
        wxDetailVo.setSpecificationList(list);
        //type Id
        Integer typeId = 0;
        Integer count = mallCollectMapper.selectByValueIdCount(id, typeId);
        wxDetailVo.setUserHasCollect(count);
        return wxDetailVo;
    }

    @Override
    public CommonData related(Integer id) {
        //开启分页功能
        PageHelper.startPage(1, 10);
        List<WxMallGoodsVo> mallGoodsVos = mallGoodsMapper.selectGoodsByIdLimitByPage(id);
        PageInfo<WxMallGoodsVo> pageInfo = new PageInfo<>(mallGoodsVos);
        return CommonData.data(pageInfo);
    }

}
